#include "global.h"

#include "console.h"

#include "console_png.h"
#include "font_png.h"

using namespace std;


Console *Console::Singleton = 0;

Console::Console(size_t LenOfList): ListLength(LenOfList) {
	texConsole = GRRLIB_LoadTexture(console_png);
	
	// Font
	texFont = GRRLIB_LoadTexture(font_png);
	GRRLIB_InitTileSet(texFont, 8, 16, 0);
}

Console::~Console() {
	MsgList.clear();
	GRRLIB_FreeTexture(texConsole);
}

void Console::Delete() {
	if(Singleton) {
		delete Singleton;
		Singleton = 0;
	}
}

Console& Console::GetConsole() {
	if(Singleton == 0)
		Singleton = new Console;
		
	return *Singleton;
}

void Console::Print(string msg) {
	if(msg.size() > 0)
		MsgList.push_back(msg);
		
	while(ListLength < MsgList.size()) {
		MsgList.pop_front();
	}
}

void Console::Pop_back() {
	MsgList.pop_back();
}

void Console::Clear() {
	MsgList.clear();
}
	
void Console::Render() {
	list<string>::const_iterator itor = MsgList.begin();
	
	GRRLIB_DrawImg (0, 4, texConsole, 0, 1, 1, RGBA(255, 255, 255, 220));
	
	for(int i = 0; itor != MsgList.end(); itor++, i++) {
		string tmp = *itor;
		char str[77] = {0};
		tmp.copy(str, 76);
		
		GRRLIB_Printf(6, 10+(14*i), texFont, GRRLIB_WHITE, 1, "%s", str);
	}
}
